<?php

namespace app\api\logic;

use app\common\exception\BusinessException;

class Article
{
    /**
     * 文章详情
     * @param $id
     * @return mixed
     * @throws
     */
    public function detail($id)
    {
        // 查询条件
        $where = [['id', '=', $id]];

        // 输出字段
        $field = '*';

        // 查询数据
        $info = app(\app\common\model\Article::class)->field($field)->where($where)->find();
        if (!$info) {
            throw new BusinessException('文章不存在或已被删除');
        }
        $info = $info->toArray();

        // 查询分类
        if ($info['cate_id']) {
            $cateInfo = app(\app\common\model\ArticleCategory::class)->field($field)->where([['id', '=', $info['cate_id']]])->find();
        }
        $info['cate_name'] = $cateInfo['name'] ?? '';

        // 更新阅读量
        app(\app\common\model\Article::class)->db()->where($where)->inc('page_view')->update();

        // 返回
        return $info;
    }

    /**
     * 查询列表
     * @param $param
     * @return array
     */
    public function getList($param): array
    {
        // 查询条件
        $where = [['status', '=', 1]];
        if (!empty($param['cate_id'])) {
            $where[] = ['cate_id', '=', $param['cate_id']];
        }

        // 输出字段
        $field = '*';

        // 查询数据
        $limit = 20;
        $list = app(\app\common\model\Article::class)->field($field)->where($where)->page(
            $param['page'],
            $limit
        )->order(['sort' => 'desc', 'create_time' => 'desc'])->select()->toArray();

        // 提取分类
        $allCateId = array_column($list, 'cate_id');
        if ($allCateId) {
            $where = [['id', 'in', $allCateId]];
            $field = 'id,name';
            $cateList = app(\app\common\model\ArticleCategory::class)->field($field)->where($where)->select()->toArray();
            $cateListDict = array_dict($cateList, 'id');
        }

        // 填充数据
        foreach ($list as $k => $v) {
            $list[$k]['cate_name'] = $cateListDict[$v['cate_id']]['name'] ?? '-';
            $list[$k]['create_date'] = formatTime($v['create_time']);
        }

        // 返回
        return $list;
    }

    /**
     * 获取首页文章
     * @return array
     */
    public function getIndexList(): array
    {
        // 查询条件
        $where = [['status', '=', 1]];

        // 输出字段
        $field = '*';

        // 查询数据
        $list = app(\app\common\model\Article::class)->field($field)->where($where)->order(['sort' => 'desc', 'create_time' => 'desc'])->select();
        $list = $list ? $list->toArray() : [];

        // 提取分类
        $allCateId = array_column($list, 'cate_id');
        if ($allCateId) {
            $where = [['id', 'in', $allCateId]];
            $field = 'id,name';
            $cateList = app(\app\common\model\ArticleCategory::class)->field($field)->where($where)->select()->toArray();
            $cateListDict = array_dict($cateList, 'id');
        }
        foreach ($list as $key => $val) {
            $list[$key]['cate_name'] = $cateListDict[$val['cate_id']]['name'] ?? '-';
            $list[$key]['create_date'] = formatTime($val['create_time']);
        }

        // 返回
        return $list;
    }
}